//
//  GCW_VideoViewController.m
//  MooMusic
//
//  Created by 管晨雯 on 15/10/22.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "GCW_VideoViewController.h"
#import "GCW_VideoCollectionViewCell.h"
#import "GCW_HeadCollectionViewCell.h"
#import "PrefixHeader.pch"
#import "NetWork.h"
#import "GCW_HeadModel.h"
#import <UIImageView+WebCache.h>
@interface GCW_VideoViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *videoCollectionView;
@property (nonatomic, strong) NSMutableArray *headArr; /* 区头数组 */
@property (nonatomic, strong) NSMutableArray *countArr;/* 数据总数组*/

@end

/* cell的重用标志 */
static NSString *const cellVideo = @"cellVideo";

/* headView的重用标志 */
static NSString *const headVideo = @"headVideo";

@implementation GCW_VideoViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.headArr = [NSMutableArray array];
        self.countArr = [NSMutableArray array];
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"电台";
    self.navigationController.navigationBar.translucent = NO;
    
    
    [self getData];
    [self creatUICollectionView];
}

#pragma mark --- 获取数据
- (void)getData
{
    
    [NetWork networkGETRequestWithURL:videoPageUrl  Paramater:nil ResultBlock:^(id result) {
        
        NSMutableDictionary *mDic = (NSMutableDictionary *)result;
        
        NSMutableArray *dataArr = [mDic objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in dataArr) {
            GCW_HeadModel *model = [[GCW_HeadModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.headArr addObject:model];
            
            NSMutableArray *temptArr = [dic objectForKey:@"data"];
            [self.countArr addObject:temptArr];
        
        }
        
        if (self.headArr.count > 0) {
            [self.videoCollectionView reloadData];
        }
    }];
}

#pragma mark --- 创建UICollectionView
- (void)creatUICollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    /* 设置方块的大小 */
    flowLayout.itemSize = CGSizeMake(160 * WIDTH, 160 *HEIGH);
    
    /* 设置方块的列间距  */
    flowLayout.minimumInteritemSpacing = 4 * WIDTH;
    
    /* 设置最小行间距 */
    flowLayout.minimumLineSpacing = 0 * HEIGH;
    flowLayout.sectionInset = UIEdgeInsetsMake(10 , 0 * HEIGH, 6 , 20* HEIGH);
    
    /* 设置头部高度 */
    flowLayout.headerReferenceSize = CGSizeMake(0, 25 * HEIGH);
    
    /* 集合视图 */
    self.videoCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    
    /* 设置代理人 */
    self.videoCollectionView.delegate = self;
    self.videoCollectionView.dataSource = self;
    
    /* 设置常用属性 */
    self.videoCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.videoCollectionView];

    
#pragma mark --- 注册cell
    [self.videoCollectionView registerClass:[GCW_VideoCollectionViewCell class] forCellWithReuseIdentifier:cellVideo];
    
    
#pragma mark ---- 注册headView
    [self.videoCollectionView registerClass:[GCW_HeadCollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:headVideo];
}

#pragma mark --- 实现协议的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.countArr[section]count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.headArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    GCW_VideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellVideo forIndexPath:indexPath];
    
    /* 赋值 */
    if (indexPath.section == 0) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
//        NSLog(@"%@",dic);

        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
    }
    
    if (indexPath.section == 1) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        //        NSLog(@"%@",dic);
        
        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
    }
    
    if (indexPath.section == 2) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        //        NSLog(@"%@",dic);
        
        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
        
    }
    
    if (indexPath.section == 3) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        //        NSLog(@"%@",dic);
        
        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
    }
    
    if (indexPath.section == 4) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        //        NSLog(@"%@",dic);
        
        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
    }
    
    
    if (indexPath.section == 5) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        //        NSLog(@"%@",dic);
        
        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
    }
    
    
    if (indexPath.section == 6) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        //        NSLog(@"%@",dic);
        
        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
    }
    
    if (indexPath.section == 7) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        //        NSLog(@"%@",dic);
        
        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
    }
    
    if (indexPath.section == 8) {
        NSArray *arr = self.countArr[indexPath.section];
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        //        NSLog(@"%@",dic);
        
        [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"large_pic_url"]] placeholderImage:nil];
        cell.typeLabel.text = [dic objectForKey:@"tag_name"];
        cell.countLabel.text = [NSString stringWithFormat:@"%@首",[dic objectForKey:@"quantity"]];
    }
    
//    NSInteger num = indexPath.item;
    cell.block = ^(NSInteger butTag){
        NSLog(@"%ld", butTag);
        
        
        
        
    };
    
    return cell;
    
}

#pragma mark --- 注册HeadView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    GCW_HeadCollectionViewCell *headView =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headVideo forIndexPath:indexPath];
    
//    headView.backgroundColor = [UIColor lightGrayColor];
    
    if (self.headArr.count > 0) {
        
        GCW_HeadModel *model = [self.headArr objectAtIndex:indexPath.section];
        headView.sortLabel.text = model.tag_type_name;
    }
   
    
    return headView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [NetWork networkGETRequestWithURL:videoPageUrl Paramater:nil ResultBlock:^(id result) {
        NSMutableArray *arr = [NSMutableArray array];
        arr = [GCW_HeadModel arrayWithModelByArray:result[@"data"]];
//        NSLog(@"~~~%@",);
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
